package com.xpm.activity;

import android.os.Handler;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.xpm.adapter.advance.ItemListViewAdapter;
import com.xpm.base.BaseRecycleViewActivity;
import com.xpm.databinding.ActivitySmartBinding;
import com.xpm.entity.DataProvider;
import com.xpm.entity.ItemBean;

import java.util.ArrayList;
import java.util.List;

public class SmartActivity extends BaseRecycleViewActivity<ItemBean,ActivitySmartBinding, ItemListViewAdapter> {


    @Override
    protected ActivitySmartBinding getBinding() {
        return ActivitySmartBinding.inflate(getLayoutInflater());
    }

    @Override
    protected SmartRefreshLayout getRefreshLayout() {
        return binding.refreshLayout;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return binding.recyclerView;
    }

    @Override
    protected ItemListViewAdapter createAdapter() {
        ItemListViewAdapter retAdapter = new ItemListViewAdapter();
        // 设置点击监听
        retAdapter.setOnItemClickListener((user, position) -> {
            // 处理点击事件
            Toast.makeText(getApplicationContext(),"点击事件："+position,Toast.LENGTH_SHORT).show();
        });

        // 设置长按监听
        retAdapter.setOnItemLongClickListener((user, position) -> {
            // 处理长按事件
            Toast.makeText(getApplicationContext(),"长按事件："+position,Toast.LENGTH_SHORT).show();
            return true;
        });
        return retAdapter;
    }
    @Override
    protected void loadData() {
        // 模拟数据加载
        new Handler().postDelayed(() -> {
            if (currentPage == INITIAL_PAGE) {
                adapter.setData(getDataList());
                finishRefresh(true);
            } else {
                adapter.addData(getDataList());
                finishLoadMore(true, currentPage >= 10); // 假设最多3页
            }
        }, 1000);
    }

    private static int count =0;
    private List<ItemBean> getDataList() {
        List<ItemBean> retData = new ArrayList<>();
        // 创建模拟数据
        for (int i = 0; i < DataProvider.icons.length; i++){
            ItemBean bean = new ItemBean();
            bean.icon =DataProvider.icons[i];
            bean.title = count+"标题"+i;
            retData.add(bean);
        }
        count++;
        return retData;

    }
}